package common.effect.match
{
	import com.common.utils.Reflection;
	import com.global.GlobalDef;
	import com.liangzhong.manager.SoundManager;
	import com.liangzhong.model.AppData;
	
	import common.effect.EffectView;
	import common.effect.IEffectView;
	
	import flash.display.Sprite;
	import flash.events.Event;
	
	import lz.controls.managers.AnimationManager;

	/**
	 *联赛效果 
	 * @author lake
	 * 
	 */	
	public class LeagueMatchEffectView extends EffectView implements IEffectView
	{
		/**
		 *数据 
		 */		
		private var _data :*;
		/**
		 * 
		 */		
		private var bgSp :Sprite;
		/**
		 *音乐索引 
		 */		
		private var sindex :int ;
		
		public function LeagueMatchEffectView()
		{
			super();
		}
		/**
		 *设置获取数据 
		 * @param value
		 * 
		 */		
		override public function set data(value :*):void
		{
			_data = value;
			initView();
		}
		override public function get data():*
		{
			return _data;
		}
		/**
		 *初始化 
		 * @param view
		 */		
		override public function initUI(view:Object=null):void
		{
			resizeEnabled = true;
		}
		/**
		 *初始化界面 
		 */		
		private function initView():void
		{
			var type :int = int(_data.type);
			switch(type)
			{
				case 1://预选赛通过
				{
					_symbolSkin = Reflection.createInstance("com.lz.fb.effect.match.trial_match_pSkin");
					if(_symbolSkin)
					{
						this.addChild(_symbolSkin);
						_symbolSkin.x = GlobalDef.GameStage.stageWidth/2;
						_symbolSkin.y = GlobalDef.GameStage.stageHeight/3;
						_symbolSkin.addEventListener(Event.REMOVED_FROM_STAGE,onRemoveFromStage);
					}
					else
					{
						destroy();
					}
					break;
				}
				case 2://甲级联赛通过
				{
					_symbolSkin = Reflection.createInstance("com.lz.fb.effect.match.base_match_pSkin");
					if(_symbolSkin)
					{
						this.addChild(_symbolSkin);
						_symbolSkin.x = GlobalDef.GameStage.stageWidth/2;
						_symbolSkin.y = GlobalDef.GameStage.stageHeight/3;
						_symbolSkin.addEventListener(Event.REMOVED_FROM_STAGE,onRemoveFromStage);
					}
					else
					{
						destroy();
					}
					break;
				}
				case 21://甲级联赛失败
				{
					_symbolSkin = Reflection.createInstance("com.lz.fb.effect.match.base_match_FSkin");
					if(_symbolSkin)
					{
						this.addChild(_symbolSkin);
						_symbolSkin.x = GlobalDef.GameStage.stageWidth/2;
						_symbolSkin.y = GlobalDef.GameStage.stageHeight/3;
						_symbolSkin.addEventListener(Event.REMOVED_FROM_STAGE,onRemoveFromStage);
					}
					else
					{
						destroy();
					}
					break;
				}
				case 31://欧冠联赛失败
				{
					_symbolSkin = Reflection.createInstance("com.lz.fb.effect.match.champion_match_FSkin");
					if(_symbolSkin)
					{
						this.addChild(_symbolSkin);
						_symbolSkin.x = GlobalDef.GameStage.stageWidth/2;
						_symbolSkin.y = GlobalDef.GameStage.stageHeight/3;
						_symbolSkin.addEventListener(Event.REMOVED_FROM_STAGE,onRemoveFromStage);
					}
					else
					{
						destroy();
					}
					break;
				}
				case 3://欧冠联赛通过
				{
					_symbolSkin = Reflection.createInstance("com.lz.fb.effect.match.champion_match_pSkin");
					if(_symbolSkin)
					{
						bgSp = new Sprite();
						bgSp.graphics.beginFill(0x000000);
						bgSp.graphics.drawRect(0,0,10,10);
						bgSp.graphics.endFill();
						bgSp.width = GlobalDef.GameStage.stageWidth;
						bgSp.height = GlobalDef.GameStage.stageHeight;
						this.addChild(bgSp);
						this.addChild(_symbolSkin);
						_symbolSkin.x = (GlobalDef.GameStage.stageWidth - _symbolSkin.width)/2;
						_symbolSkin.y = (GlobalDef.GameStage.stageHeight - _symbolSkin.height)/2;
						_symbolSkin.addEventListener(Event.REMOVED_FROM_STAGE,onRemoveFromStage);
						//music
						sindex = int(Math.random() * 3);
						SoundManager.getInstance().stopMusic("com.football.effect.sound.BgMusic_1");
						SoundManager.getInstance().playMusic("com.football.effect.sound.LegendMusic_" + sindex,false);
					}
					else
					{
						destroy();
					}
					break;
				}
				case 4://传奇通过
				{
					_symbolSkin = Reflection.createInstance("com.lz.fb.effect.match.legend_match_pSkin");
					if(_symbolSkin)
					{
						this.addChild(_symbolSkin);
						_symbolSkin.x = GlobalDef.GameStage.stageWidth/2;
						_symbolSkin.y = GlobalDef.GameStage.stageHeight/3;
						_symbolSkin.addEventListener(Event.REMOVED_FROM_STAGE,onRemoveFromStage);
					}
					else
					{
						destroy();
					}
					break;
				}
				case 41://传奇失败
				{
					_symbolSkin = Reflection.createInstance("com.lz.fb.effect.match.legend_match_FSkin");
					if(_symbolSkin)
					{
						this.addChild(_symbolSkin);
						_symbolSkin.x = GlobalDef.GameStage.stageWidth/2;
						_symbolSkin.y = GlobalDef.GameStage.stageHeight/3;
						_symbolSkin.addEventListener(Event.REMOVED_FROM_STAGE,onRemoveFromStage);
					}
					else
					{
						destroy();
					}
					break;
				}
				default:
				{
					destroy();
				}
			}
		}
		/**
		 *自适应 
		 * @param e
		 * 
		 */		
		override protected function onResizeHandler(e :Event):void
		{
			var type :int = int(_data.type);
			switch(type)
			{
				case 1://预选赛通过
				case 2://甲级联赛通过
				case 4://传奇通过
				case 21:
				case 31:
				case 41:
				{
					if(_symbolSkin)
					{
						_symbolSkin.x = GlobalDef.GameStage.stageWidth/2;
						_symbolSkin.y = GlobalDef.GameStage.stageHeight/3;
					}
					break;
				}
				case 3://欧冠通过
				{
					if(_symbolSkin)
					{
						if(bgSp)
						{
							bgSp.width = GlobalDef.GameStage.stageWidth;
							bgSp.height = GlobalDef.GameStage.stageHeight;
						}
						_symbolSkin.x = (GlobalDef.GameStage.stageWidth - _symbolSkin.width)/2;
						_symbolSkin.y = (GlobalDef.GameStage.stageHeight - _symbolSkin.height)/2;
					}
					break;
				}
			}
		}
		/**
		 *移除 
		 * @param e
		 * 
		 */		
		private function onRemoveFromStage(e :Event):void
		{
			destroy();
		}
		/**
		 *销毁 
		 */		
		override public function destroy():void
		{
			resizeEnabled = false;
			var type :int = int(_data.type);
			if(type == 3)
			{
				SoundManager.getInstance().stopMusic("com.football.effect.sound.LegendMusic_" + sindex);
				SoundManager.getInstance().playMusic("com.football.effect.sound.BgMusic_1",true);
			}
			//AppData.getInstance().leagueMatchManager.setNextType();
			AnimationManager.remove();
			if(_symbolSkin)
			{
				_symbolSkin.removeEventListener(Event.REMOVED_FROM_STAGE,onRemoveFromStage);
			}
			if(bgSp && bgSp.parent)
			{
				bgSp.parent.removeChild(bgSp);
			}
			if(this.parent)
			{
				this.parent.removeChild(this);
			}
		}
	}
}